
var initQueryTables = function(){
	$('#enterprise').dataTable({
		"bProcessing" : true, //DataTables载入数据时，是否显示‘进度’提示
        "bServerSide" : true, //是否启动服务器端数据导入
        "bStateSave" : false, //是否打开客户端状态记录功能,此功能在ajax刷新纪录的时候不会将个性化设定回复为初始化状态
        "bJQueryUI" : true, //是否使用 jQury的UI theme
        "aLengthMenu" : [5, 10, 15], //更改显示记录数选项
        "iDisplayLength" : 10, //默认显示的记录数
        "bAutoWidth" : true, //是否自适应宽度 update\
        "bScrollCollapse" : false, //是否开启DataTables的高度自适应，当数据条数不够分页数据条数的时候，插件高度是否随数据条数而改变
        "bPaginate" : true, //是否显示（应用）分页器
        "bInfo" : true, //是否显示页脚信息，DataTables插件左下角显示记录数
        "sPaginationType" : "full_numbers", //详细分页组，可以支持直接跳转到某页
        "bSort" : true, //是否启动各个字段的排序功能
        "aaSorting" : [[1, "DESC"]], //默认的排序方式，第3列，升序排列
        "bFilter" : false, //是否启动过滤、搜索功能
        "sScrollX": "100%",
		"sScrollXInner": 1650,
        "sPaginationType": "full_numbers",
	    "sAjaxSource":'sys/SysAccessory/list',
        "fnServerData":retrieveData,  //自定义数据获取函数
		"aoColumns" : [ {
			"mDataProp" : "uid",
			"sTitle" : "操作",
			"bSortable": false,
			"sDefaultContent" : "",
			"sClass" : "center",
			"mRender": function (data, display, row) {
				 var id = row.id,loanId=row.loanId,clientId=row.clientId;
			     return top._set_default_href([id,loanId,clientId],[{'title':'下载','func':'down_load'},{'title':'编辑','func':'default_edit'},{'title':'删除','func':'default_del'}]);
			}
			
		}, {
			"mDataProp" : "accessoryTheme",
			"sTitle" : "主题",
			"sDefaultContent" : "",
			"sClass" : "center",
			"sWidth":"10%"
		}, {
			"mDataProp" : "ownerType",
			"sTitle" : "所属业务类型",
			"sDefaultContent" : "",
			"sClass" : "center",
			"mRender": function (data, display, row) {
		        return SYS_BASE_DATA['Accessory_Loan_ownerType_Code'][data]||'';
		     }
		},{
			"mDataProp" : "accessoryClass",
			"sTitle" : "附件类型",
			"sDefaultContent" : "",
			"sClass" : "center",
			"mRender": function (data, display, row) {
		        return SYS_BASE_DATA['Accessory_Class_Apply_Code'][data]||'';
		     }
		},{
			"mDataProp" : "accessoryName",
			"sTitle" : "文件名",
			"sDefaultContent" : "",
			"sClass" : "center",
			"mRender": function (data, display, row) {
		        return '<a href="javascript:down_load('+row.id+');">'+data+'</a>';
		     }
		}, {
			"mDataProp" : "accessoryFileSize",
			"sTitle" : "文件大小",
			"sDefaultContent" : "",
			"sClass" : "center",
			"mRender": function (data, display, row) {
				var accessoryFileUnit = row.accessoryFileUnit;
		        return data+''+accessoryFileUnit;
		     }
		},{
			"mDataProp" : "loanCode",
			"sTitle" : "贷款编号",
			"sDefaultContent" : "",
			"sClass" : "center"
		},{
			"mDataProp" : "clientName",
			"sTitle" : "所属客户",
			"sDefaultContent" : "",
			"sClass" : "center"
		}, {
			"mDataProp" : "addUserName",
			"sTitle" : "上传人",
			"sDefaultContent" : "",
			"sClass" : "center",
			"mRender": function (data, display, row) {
		        return data||'ADMIN';
		     }
		}, {
			"mDataProp" : "addTime",
			"sTitle" : "上传时间",
			"sDefaultContent" : "",
			"sClass" : "center"
		}, {
			"mDataProp" : "accessoryRemark",
			"sTitle" : "备注",
			"sDefaultContent" : "",
			"sClass" : "center"
			
		}
		  
		],
		"oLanguage": {
            "sUrl":"static_resources/language/lang-cn.txt" //这个是国际化，也就是汉化的各种文字数据源，写对url就行了
        }
        
	});
	
};

//自定义数据获取函数
var retrieveData = function( sSource, aoData, fnCallback ) {
	var array = $("#_form_search_default").serializeArray();
	aoData = top._set_query_conditions(aoData,array);
	aoData.push({'name':'v2hldghlcib0agugzgf0ysbhy2nlc3m','value':1});
	        $.ajax( {
	                type: "POST", 
	                url: sSource, 
	                cache : false,
	                dataType : "json",
	                data : aoData, 
	                success: function(resp) {
	                	if(resp && resp.state == 200){
	                		fnCallback(resp.datagrid);
	                		return false;
	                	}
	                	fnCallback(null);
	            }
	        });
}

var down_load = function(id){
	window.location.href = 'sys/SysAccessoryExt/packageAccessory?id='+id;
}


var default_del = function(id){
	$.dialog.confirm('删除的附件不可恢复,确定删除？', function(){
		$.ajax({
			url:'sys/SysAccessoryExt/delete',
			data:{pks:id},
			type:'post',
			dataType:'json',
			success:function(result){
				if(result && result.state=="200"){
					$.dialog.alert('删除成功!',function(){
						$('#enterprise').dataTable().fnDraw();
					});
					return false;
				}
				$.dialog.alert('删除失败!',function(){
				});
				
			},
			complete:function(){
			}
		});
	});
	
};

var default_edit = function(id,loanId,clientId){
	$.dialog.confirm('以前的附件将被覆盖，确定修改附件吗？', function(){
		$.dialog({
			id:'_accessory_upload_dailog',
			title:'上传/编辑附件',
			lock:true,
			width:700,
			height:400,
			content:'url:'+parent.basePath+'jsp/loan/accessory_upload.jsp?loanId='+loanId+'&clientId='+clientId+'&id='+id,
			close: function(){
				$('#enterprise').dataTable().fnDraw();
		    }
		});
	});
}

$(function(){
	$('#accessoryClass').mSelect({
		myClass:'scselect',
		width:192,
		baseCode:'Accessory_Class_Apply_Code',
		isAllSelect:true
		
	});
	
	$('#ownerType').mSelect({
		myClass:'scselect',
		baseCode:'Accessory_Loan_ownerType_Code',
		isAllSelect:true,
		onchageCallback:function(text,value){
              if(value == 'HT'){
            	  $('#accessoryClass').mSelect({
            			myClass:'scselect',
            			width:192,
            			baseCode:'Accessory_Class_Contract_Code',
            			isAllSelect:true
            		});
              }else{
            	  $('#accessoryClass').mSelect({
            			myClass:'scselect',
            			width:192,
            			baseCode:'Accessory_Class_Apply_Code',
            			isAllSelect:true
            			
            		});
              }
		}
	});
	
	

	//查询事件
	$('input[name="query_btn"]').click(function(){
		$('#enterprise').dataTable().fnDraw();
	});

	$('input[name="confirm_button"]').click(function(){
		$('#_form_default').submit();
	});
	initQueryTables();

	
});
